package com.dhcy.mqtt.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcy.mqtt.config.ProjectConfig;
import com.dhcy.mqtt.dao.EquipmentMapper;
import com.dhcy.mqtt.model.Equipment;
import com.dhcy.mqtt.model.constant.MqttConstant;
import com.dhcy.mqtt.par.ht.EquipmentAddPar;
import com.dhcy.mqtt.par.ht.EquipmentListPar;
import com.dhcy.mqtt.par.ht.HandleDto;
import com.dhcy.mqtt.result.ht.EquipmentResult;
import com.dhcy.mqtt.result.ht.TableResultData;
import com.dhcy.mqtt.service.AsynService;
import com.dhcy.mqtt.service.EquipmentService;
import com.dhcy.mqtt.util.AppResultData;
import com.dhcy.mqtt.util.IsNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService {
    @Autowired
    EquipmentMapper equipmentMapper;
    @Autowired
    AsynService asynService;
    @Autowired
    ProjectConfig projectConfig;

    /**
     * 新增设备
     *
     * @param equipmentaddPar
     * @return
     */
    @Override
    public AppResultData equipmentAdd(EquipmentAddPar equipmentaddPar) {
        Equipment equipment = new Equipment();
        BeanUtils.copyProperties(equipmentaddPar, equipment);
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("number", equipment.getNumber());
        queryWrapper.eq("is_del",false);
        List<Equipment> equipmentList = equipmentMapper.selectList(queryWrapper);
        if (equipmentList.size() > 0 ) {
            return AppResultData.errorMessage(equipment.getNumber()+"号设备已存在");
        }
//        equipment.setCreateTime(new Date());
//        equipment.setUpdateTime(new Date());
//        equipment.setHandle(projectConfig.getPrefix()+"/"+equipment.getNumber());
//        equipmentMapper.insert(equipment);
        HandleDto handleDto = new HandleDto(){{
            setDeviceNumber(equipment.getNumber());
        }};
        asynService.regIden(handleDto,1);
        return AppResultData.successMessage("成功");
    }

    /**
     * 查询设备list
     */
    @Override
    public TableResultData equipmentList(EquipmentListPar equipmentListPar) {

        IPage<Equipment> page = new Page<>(equipmentListPar.getPage(), equipmentListPar.getPageSize());
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();

//        if (!IsNull.isNullOrEmpty(equipmentListPar.getCode())) {
//            queryWrapper.like(" code ", equipmentListPar.getCode());
//        }
//        if (!IsNull.isNullOrEmpty(equipmentListPar.getName())) {
//            queryWrapper.like(" name ", equipmentListPar.getName());
//        }
//        if (!IsNull.isNullOrEmpty(equipmentListPar.getNumber())) {
//            queryWrapper.like(" number ", equipmentListPar.getNumber());
//        }
//        if (!IsNull.isNullOrEmpty(equipmentListPar.getOnline())) {
//            queryWrapper.like(" online  ", equipmentListPar.getOnline());
//        }
        queryWrapper.orderByDesc("id");
        // List<Equipment> equipmentList = equipmentMapper.selectList(queryWrapper);
        List<EquipmentResult> resultList = equipmentMapper.list(page, queryWrapper);
        return TableResultData.success(page.getTotal(), resultList);
    }

    @Override
    public AppResultData equipmentDel(Integer id) {
        int count = equipmentMapper.deleteById(id);
        if (count > 0)
            return AppResultData.successMessage("成功");
        else {
            return AppResultData.errorMessage("当前记录不存在");
        }
    }

    /**
     * 设备同步
     * @param list
     * @return
     */
//    @Override
//    public AppResultData asynEquipment(List<Equipment> list) {
//        if (ObjectUtil.isNotEmpty(list)){
//            list.stream().forEach(item->{
//                Equipment equipment = equipmentMapper.selectOne(new QueryWrapper<Equipment>().eq("code", item.getCode()).eq("number", item.getNumber()));
//                if (ObjectUtil.isEmpty(equipment)){
//                    item.setId(null);
//                    equipmentMapper.insert(item);
//                }
//            });
//        }
//        return AppResultData.successMessage("成功");
//    }


}
